<template>
    <!-- 对话框表单 -->
    <el-dialog title="编辑" v-model="visible" :close-on-click-modal="false" :destroy-on-close="true" @close="closeFun">
        <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="name">
                <el-input v-model="formData.name" disabled />
            </el-form-item>
            <el-form-item label="缩写" prop="short">
                <el-input v-model="formData.short" disabled />
            </el-form-item>
            <el-form-item label="SEO标题" prop="page_title">
                <el-input v-model="formData.page_title" placeholder="请输入SEO标题" maxlength="500" clearable />
            </el-form-item>
            <el-form-item label="SEO关键词" prop="page_keywords">
                <el-input v-model="formData.page_keywords" placeholder="请输入SEO关键词" maxlength="500" clearable />
            </el-form-item>
            <el-form-item label="SEO描述" prop="page_description">
                <el-input type="textarea" v-model="formData.page_description" placeholder="请输入SEO描述" maxlength="500"
                    autosize clearable />
            </el-form-item>
            <el-row>
                <el-col style="text-align: center;" :span="24">
                    <el-button @click="closeFun()">取消</el-button>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                </el-col>
            </el-row>
        </el-form>
    </el-dialog>
</template>

<script setup>
import { ref, reactive, defineExpose, inject, defineEmits } from 'vue';
import { updateTagIndex } from '@/api/tag';

let emit = defineEmits(['update']);
let used = inject('used');//公共方法
let visible = ref(false);//是否显示
let formData = reactive({});//表单数据
let ruleFormRef = ref(null);//表单
//表单验证
const rules = reactive(
    {
        page_title: [
            { required: true, message: '请输入SEO标题', trigger: 'blur' },
        ],
        page_keywords: [
            { required: true, message: '请输入SEO关键字', trigger: 'blur' },
        ],
        page_description: [
            { required: true, message: '请输入SEO描述', trigger: 'blur' },
        ],
    }
)
//显示
const show = (row) => {
    Object.assign(formData, row);
    visible.value = true;
};
//关闭
const closeFun = () => {
    visible.value = false;
};

//提交
const submitForm = async (formEl) => {
    let params = used.deepClone(formData)
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            let res = await updateTagIndex(params, 'post');
            if (res.code == 1) {
                used.msgSuccess('更新成功')
                closeFun();
                emit('update')
            } else {
                used.msgError(res.message)
            }
        } else {
            console.log('error submit!', fields)
        }
    })
}
//
defineExpose({
    show
})
</script>

<style scoped lang="scss"></style>
